﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace SrWinGui
{
    public partial class MainForm : Form
    {
        private frmShoutcast formShoutcast;
        private frmRipping formRipping;
        private string stato = "";

        public MainForm()
        {
            InitializeComponent();
            formShoutcast = new frmShoutcast();
            formRipping = new frmRipping();
            formShoutcast.Dock = formRipping.Dock = DockStyle.Fill;
            formShoutcast.TopLevel = formRipping.TopLevel = false;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            launchFormShoutcast();
            checkOptions();
            Impostazioni.Instance.GetArguments();
        }

        /// <summary>
        /// Checks if xml files with settings exists
        /// Yes: load settings
        /// No: create file with default settings and load them
        /// </summary>
        private void checkOptions()
        {
            if (!File.Exists("SrwSettings.xml"))
                Impostazioni.Instance.Inizializza();
            else
                Impostazioni.Instance.Load();
        }


        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Ripper.Instance.KillRipping();
        }

        private void uxShoutcastStations_Click(object sender, EventArgs e)
        {
            launchFormShoutcast();
        }

        /// <summary>
        /// launchs shoutcast station search form
        /// </summary>
        private void launchFormShoutcast()
        {
            if (splitContainer1.Panel2.Controls.Contains(formShoutcast))
                return;

            splitContainer1.Panel2.Controls.Clear();            
            splitContainer1.Panel2.Controls.Add(formShoutcast);
            formShoutcast.Show();
        }
        /// <summary>
        /// launch ripping status form
        /// </summary>
        private void uxRippingStatus_Click(object sender, EventArgs e)
        {
            if (splitContainer1.Panel2.Controls.Contains(formRipping))
                return;

            splitContainer1.Panel2.Controls.Clear();            
            splitContainer1.Panel2.Controls.Add(formRipping);
            formRipping.Show();
        }
        #region Status Label Management
        /// <summary>
        /// Set status Label in the bottom of the form
        /// </summary>
        /// <param name="msg"></param>
        public void SetStatus(string msg)
        {
            uxStatusLable.Text = msg;
        }
        private void setStatus(string msg)
        {
            uxStatusLable.Text = msg;
        }
        private delegate void setStatusDelegate(string msg);
        #endregion

        /// <summary>
        /// Launchs ripping from selected shoutcast station
        /// </summary>
        /// <param name="id">Shoutcast station ID</param>
        public void LaunchRipping(string id)
        {
            splitContainer1.Panel2.Controls.Clear();
            splitContainer1.Panel2.Controls.Add(formRipping);
            formRipping.Show(id);
        }

        private void uxOptions_Click(object sender, EventArgs e)
        {
            frmOptions f = new frmOptions();
            f.ShowDialog();
        }

    }
}